/**
 * Author:Andy
 * Date:2023/3/20 16:19
 * Description:EditText
 */

import android.text.Spannable
import android.text.SpannableString
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.text.style.TypefaceSpan
import android.view.MotionEvent
import android.widget.EditText

/**
 * 对hit进一步定制化
 *
 * @param color 颜色值
 * @param fontSize 文本大小，sp值
 * @param fontFamily 字体家族。res/font内的字体名称
 */
fun EditText.customHint(color: Int, fontSize: Int, fontFamily: String) {
    val spString = SpannableString(hint)
    spString.setSpan(
        ForegroundColorSpan(color),
        0,
        spString.length,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
    )

    spString.setSpan(
        AbsoluteSizeSpan(fontSize.toFloat().sp2px().toInt()),
        0,
        spString.length,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
    )

    spString.setSpan(
        TypefaceSpan(fontFamily), 0,
        spString.length,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
    )

    hint = spString
}

/**
 * edittext 页面点击外部区域判断是否需要隐藏软键盘
 */
fun EditText.isHideInput(ev: MotionEvent): Boolean {
    val location = intArrayOf(0, 0)
    getLocationInWindow(location)
    val left = location[0]
    val top = location[1]
    val bottom = top + height
    val right = left + width
    return !(ev.x > left && ev.x < right && ev.y > top && ev.y < bottom)
}